package Servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

import Bean.User;
import Dao.UserDao;

/**
 * Servlet implementation class JudgeRole
 */
public class JudgeRole extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JudgeRole() {}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html charset = UTF-8");
		UserDao userDao = new UserDao();
		User user = null;
		String username = (String) request.getAttribute("username");
		try {
			user = userDao.selectByUsername(username);
		} catch (Exception e) {
			System.out.println("wrong");
		}
		if(user.getRole() == 1) {
			request.getRequestDispatcher("admin_info.jsp").forward(request, response);
		} else {
			request.getRequestDispatcher("info.jsp").forward(request, response);
		}
	}

}
